package com.bls.cate.ui.activity.attention;

import android.content.DialogInterface;
import android.content.Intent;
import android.support.v7.widget.DividerItemDecoration;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;

import com.bls.blslib.base.BaseActivity;
import com.bls.blslib.eventbus.Event;
import com.bls.cate.R;
import com.bls.cate.adapter.AttentionListAdapter;
import com.bls.cate.constant.EventBusCode;
import com.bls.cate.constant.Interface;
import com.bls.cate.model.AttentionListRes;
import com.bls.cate.ui.activity.person_other.PersonOtherActivity;
import com.bls.cate.ui.activity.search_user.SearchUserActivity;
import com.bls.cate.ui.widget.MDialog;
import com.bls.cate.ui.widget.SwipeItemLayout;
import com.bls.cate.ui.widget.TitleView;
import com.bls.cate.utils.AccountUtil;
import com.lzy.okgo.cache.CacheMode;
import com.lzy.okgo.model.HttpParams;
import com.lzy.okgo.model.Response;
import com.lzy.okgo.request.base.Request;

import java.util.ArrayList;
import java.util.List;

/**
 * 　　  () 　　　  ()
 * 　　  ( ) 　　　( )
 * 　　  ( ) 　　　( )
 * 　　┏┛┻━━━┛┻┓
 * 　　┃　　　━　　　┃
 * 　　┃　┳┛　┗┳　┃
 * 　　┃　　　┻　　　┃
 * 　　┗━┓　　　┏━┛
 * 　　　　┃　　　┃
 * 　　　　┃　　　┗━━━┓
 * 　　　　┃　　　　　　　┣┓
 * 　　　　┃　　　　　　　┏┛
 * 　　　　┗┓┓┏━┳┓┏┛
 * 　　　　　┃┫┫　┃┫┫
 * 　　　　　┗┻┛　┗┻┛
 * ——————————————————
 * Created by BinglingSu on 2018/1/24.
 */

public class AttentionActivity extends BaseActivity implements TitleView.OnTitleClickListener, AttentionListAdapter.AttentionListAdapterListener {

    private TitleView titleView;
    private RecyclerView rvList;

    private AttentionActivity mActivity;
    private List<AttentionListRes.ResultListDataBean> resultListDataBeanList;
    private AttentionListAdapter attentionListAdapter;
    private String uniqueId;

    @Override
    protected void initParams() {
        uniqueId = AccountUtil.getUniqueId();
        mActivity = (AttentionActivity) getActivity();
        resultListDataBeanList = new ArrayList<>();
    }

    @Override
    protected int getLayoutId() {
        return R.layout.activity_attention;
    }

    @Override
    protected void initFvb() {
        titleView = findViewById(R.id.title_view);
        rvList = findViewById(R.id.rv_list);
    }

    @Override
    protected void initTitle() {
        titleView.setTitleMiddleText("关注列表");
        titleView.setBtnMode(1, 2);
        titleView.titleRightTv.setText("查找");
        titleView.setTitleClickListener(this);
    }

    @Override
    protected void initView() {
        LinearLayoutManager layoutManager = new LinearLayoutManager(mActivity);
        rvList.setLayoutManager(layoutManager);
        attentionListAdapter = new AttentionListAdapter(mActivity, resultListDataBeanList, this);
        rvList.addItemDecoration(new DividerItemDecoration(mActivity, LinearLayoutManager.VERTICAL));
        rvList.setAdapter(attentionListAdapter);
    }

    @Override
    protected void initListener() {
        SwipeItemLayout.OnSwipeItemTouchListener onSwipeItemTouchListener = new SwipeItemLayout.OnSwipeItemTouchListener(mActivity);
        rvList.addOnItemTouchListener(onSwipeItemTouchListener);
    }

    @Override
    protected void initData() {
        presenter.requestStrData(0, Interface.INTERFACE_ATTENTION, CacheMode.NO_CACHE, new HttpParams("RequestMethod", "AttentionList"), new HttpParams("UniqueId", uniqueId));
    }

    @Override
    protected void initResume() {
        presenter.requestStrData(0, Interface.INTERFACE_ATTENTION, CacheMode.NO_CACHE, new HttpParams("RequestMethod", "AttentionList"), new HttpParams("UniqueId", uniqueId));
    }

    @Override
    public void showOnStart(Request request, int requestId) {
        loadingDogDialog.show();
    }

    @Override
    public void showOnSuccess(int requestId, String responseBody, Response response) {
        switch (requestId) {
            case 0:
                AttentionListRes res = gson.fromJson(responseBody, AttentionListRes.class);
                if (res.isResultStatus()) {
                    resultListDataBeanList = res.getResultListData();
                    attentionListAdapter.setNewData(resultListDataBeanList);
                }
                break;
        }
    }

    @Override
    public void showOnFinish(int requestId) {
        loadingDogDialog.dismiss();
    }

    @Override
    public void onTitleClick(int btnType) {
        switch (btnType) {
            case 1:
                finish();
                break;
            case 4:
                Intent intent = new Intent();
                intent.putExtra("UniqueId", uniqueId);
                intent.setClass(mActivity, SearchUserActivity.class);
                mActivity.startActivity(intent);
                break;
        }
    }

    @Override
    public void onAttentionListAdapterListener(AttentionListAdapter.ButtonType buttonType, final int position) {
        if (buttonType == AttentionListAdapter.ButtonType.RL_ROOT) {
            AttentionListRes.ResultListDataBean resultListDataBean = resultListDataBeanList.get(position);
            Intent intent = new Intent(mActivity, PersonOtherActivity.class);
            intent.putExtra("fromPage", "AttentionActivity");
            intent.putExtra("resultListDataBean", resultListDataBean);
            mActivity.startActivity(intent);
        } else if (buttonType == AttentionListAdapter.ButtonType.BTN_DEL) {
            AttentionListRes.ResultListDataBean resultListDataBean = resultListDataBeanList.get(position);
            final String otherUniqueId = resultListDataBean.getOtherUniqueId();
            String dialogMessage = "确定取消对 " + resultListDataBean.getOtherNickName() + " 的关注么？";
            MDialog.showCommonDialog(mActivity, "取消关注", dialogMessage, "确定", "取消", 1, new MDialog.AlertDialogUtilListener() {
                @Override
                public void MyAlertDialogListener(DialogInterface dialog, int dialogFlag, MDialog.MDialogBtnEnum btnEnum) {
                    if (btnEnum == MDialog.MDialogBtnEnum.CONFIRM) {
                        attentionListAdapter.remove(position);
                        presenter.requestStrData(1, Interface.INTERFACE_ATTENTION, CacheMode.NO_CACHE, new HttpParams("RequestMethod", "AttentionDel"), new HttpParams("UniqueId", uniqueId), new HttpParams("OtherUniqueId", otherUniqueId));
                    }
                }
            });
        }
    }


    @Override
    protected boolean isRegisterEventBus() {
        return true;
    }

    @Override
    protected void receiveStickyEvent(Event event) {
        super.receiveStickyEvent(event);
        if (event.getCode() == EventBusCode.AttentionList_Refresh)
            presenter.requestStrData(0, Interface.INTERFACE_ATTENTION, CacheMode.NO_CACHE, new HttpParams("RequestMethod", "AttentionList"), new HttpParams("UniqueId", uniqueId));
    }
}
